/**
 * 作者: hxl
 * 日期: 2024-12-18 14:07:03
 * 备注: 用户七天乐活动表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserIapGiftPush;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserIapGiftPushModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.iap_gift_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // 玩家id
    protected long userId; // 用户id
    protected int iapGiftId; // 礼包id
    protected long endTime; // 推送结束时间
    protected long startTime; // 开始推送时间
    protected int buyNum; // 已购买次数
    protected int triggerNum; // 已触发次数
    protected int pushBuyNum; // 推送期间购买次数
    protected int pushTriggerNum; // 推送期间已触发次数
    protected int triggerNumDay; // 每日已触发次数
    protected long resetTimeDay; // 每日重置时间
    protected int mainTriggerNum; // 主推送触发次数
    protected long triggerTime; // 触发时间
    protected String buyList = ""; // 已购买的子礼包

    public void reset() {
    }

    protected String getKey() {
        return "UserIapGiftPush." + userId + "." + iapGiftId;
    }

    protected String getListKey() {
        return "UserIapGiftPush." + userId;
    }

    protected String getListItemKey() {
        return iapGiftId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_iap_gift_push"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 玩家id
        obj.put("userId", userId); // 用户id
        obj.put("iapGiftId", iapGiftId); // 礼包id
        obj.put("endTime", endTime); // 推送结束时间
        obj.put("startTime", startTime); // 开始推送时间
        obj.put("buyNum", buyNum); // 已购买次数
        obj.put("triggerNum", triggerNum); // 已触发次数
        obj.put("pushBuyNum", pushBuyNum); // 推送期间购买次数
        obj.put("pushTriggerNum", pushTriggerNum); // 推送期间已触发次数
        obj.put("triggerNumDay", triggerNumDay); // 每日已触发次数
        obj.put("resetTimeDay", resetTimeDay); // 每日重置时间
        obj.put("mainTriggerNum", mainTriggerNum); // 主推送触发次数
        obj.put("triggerTime", triggerTime); // 触发时间
        obj.put("buyList", buyList); // 已购买的子礼包
        return obj;
    }

    public PBUserIapGiftPush.Builder takeInitBuilder() {
        PBUserIapGiftPush.Builder sb = PBUserIapGiftPush.newBuilder();
        if (iapGiftId != 0) {
            sb.setIapGiftId(iapGiftId);
        }
        if (endTime != 0) {
            sb.setEndTime(endTime);
        }
        if (startTime != 0) {
            sb.setStartTime(startTime);
        }
        if (buyNum != 0) {
            sb.setBuyNum(buyNum);
        }
        if (pushTriggerNum != 0) {
            sb.setPushTriggerNum(pushTriggerNum);
        }
        if (triggerNumDay != 0) {
            sb.setTriggerNumDay(triggerNumDay);
        }
        if (resetTimeDay != 0) {
            sb.setResetTimeDay(resetTimeDay);
        }
        if (mainTriggerNum != 0) {
            sb.setMainTriggerNum(mainTriggerNum);
        }
        if (triggerTime != 0) {
            sb.setTriggerTime(triggerTime);
        }
        if (this.buyList != null && this.buyList.length() > 0) {
            JsonArray list = new JsonArray(this.buyList);
            for(int i=0; i<list.size(); i++) {
                sb.addBuyList(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserIapGiftPush.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserIapGiftPush.Builder sb = PBUserIapGiftPush.newBuilder();
        if (isFieldUpdate("iapGiftId")) {
            sb.setIapGiftId(iapGiftId);
        }
        if (isFieldUpdate("endTime")) {
            sb.setEndTime(endTime);
        }
        if (isFieldUpdate("startTime")) {
            sb.setStartTime(startTime);
        }
        if (isFieldUpdate("buyNum")) {
            sb.setBuyNum(buyNum);
        }
        if (isFieldUpdate("pushTriggerNum")) {
            sb.setPushTriggerNum(pushTriggerNum);
        }
        if (isFieldUpdate("triggerNumDay")) {
            sb.setTriggerNumDay(triggerNumDay);
        }
        if (isFieldUpdate("resetTimeDay")) {
            sb.setResetTimeDay(resetTimeDay);
        }
        if (isFieldUpdate("mainTriggerNum")) {
            sb.setMainTriggerNum(mainTriggerNum);
        }
        if (isFieldUpdate("triggerTime")) {
            sb.setTriggerTime(triggerTime);
        }
        if (isFieldUpdate("buyList")) {
            if (this.buyList != null && this.buyList.length() > 0) {
                JsonArray list = new JsonArray(this.buyList);
                for(int i=0; i<list.size(); i++) {
                    sb.addBuyList(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearBuyList();
                }
            } else {
                sb.clearBuyList();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getIapGiftId() {
        return iapGiftId;
    }

    public void putIapGiftId(int iapGiftId) {
        if (this.iapGiftId != iapGiftId) {
            this.iapGiftId = iapGiftId;
            updateRedisHashField(getKey(), "iapGiftId", this.iapGiftId);
        }
    }

    public void addIapGiftId(int value) {
        this.iapGiftId += value;
        updateRedisHashField(getKey(), "iapGiftId", this.iapGiftId);
    }

    public long getEndTime() {
        return endTime;
    }

    public void putEndTime(long endTime) {
        if (this.endTime != endTime) {
            this.endTime = endTime;
            updateRedisHashField(getKey(), "endTime", this.endTime);
        }
    }

    public void addEndTime(long value) {
        this.endTime += value;
        updateRedisHashField(getKey(), "endTime", this.endTime);
    }

    public long getStartTime() {
        return startTime;
    }

    public void putStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            updateRedisHashField(getKey(), "startTime", this.startTime);
        }
    }

    public void addStartTime(long value) {
        this.startTime += value;
        updateRedisHashField(getKey(), "startTime", this.startTime);
    }

    public int getBuyNum() {
        return buyNum;
    }

    public void putBuyNum(int buyNum) {
        if (this.buyNum != buyNum) {
            this.buyNum = buyNum;
            updateRedisHashField(getKey(), "buyNum", this.buyNum);
        }
    }

    public void addBuyNum(int value) {
        this.buyNum += value;
        updateRedisHashField(getKey(), "buyNum", this.buyNum);
    }

    public int getTriggerNum() {
        return triggerNum;
    }

    public void putTriggerNum(int triggerNum) {
        if (this.triggerNum != triggerNum) {
            this.triggerNum = triggerNum;
            updateRedisHashField(getKey(), "triggerNum", this.triggerNum);
        }
    }

    public void addTriggerNum(int value) {
        this.triggerNum += value;
        updateRedisHashField(getKey(), "triggerNum", this.triggerNum);
    }

    public int getPushBuyNum() {
        return pushBuyNum;
    }

    public void putPushBuyNum(int pushBuyNum) {
        if (this.pushBuyNum != pushBuyNum) {
            this.pushBuyNum = pushBuyNum;
            updateRedisHashField(getKey(), "pushBuyNum", this.pushBuyNum);
        }
    }

    public void addPushBuyNum(int value) {
        this.pushBuyNum += value;
        updateRedisHashField(getKey(), "pushBuyNum", this.pushBuyNum);
    }

    public int getPushTriggerNum() {
        return pushTriggerNum;
    }

    public void putPushTriggerNum(int pushTriggerNum) {
        if (this.pushTriggerNum != pushTriggerNum) {
            this.pushTriggerNum = pushTriggerNum;
            updateRedisHashField(getKey(), "pushTriggerNum", this.pushTriggerNum);
        }
    }

    public void addPushTriggerNum(int value) {
        this.pushTriggerNum += value;
        updateRedisHashField(getKey(), "pushTriggerNum", this.pushTriggerNum);
    }

    public int getTriggerNumDay() {
        return triggerNumDay;
    }

    public void putTriggerNumDay(int triggerNumDay) {
        if (this.triggerNumDay != triggerNumDay) {
            this.triggerNumDay = triggerNumDay;
            updateRedisHashField(getKey(), "triggerNumDay", this.triggerNumDay);
        }
    }

    public void addTriggerNumDay(int value) {
        this.triggerNumDay += value;
        updateRedisHashField(getKey(), "triggerNumDay", this.triggerNumDay);
    }

    public long getResetTimeDay() {
        return resetTimeDay;
    }

    public void putResetTimeDay(long resetTimeDay) {
        if (this.resetTimeDay != resetTimeDay) {
            this.resetTimeDay = resetTimeDay;
            updateRedisHashField(getKey(), "resetTimeDay", this.resetTimeDay);
        }
    }

    public void addResetTimeDay(long value) {
        this.resetTimeDay += value;
        updateRedisHashField(getKey(), "resetTimeDay", this.resetTimeDay);
    }

    public int getMainTriggerNum() {
        return mainTriggerNum;
    }

    public void putMainTriggerNum(int mainTriggerNum) {
        if (this.mainTriggerNum != mainTriggerNum) {
            this.mainTriggerNum = mainTriggerNum;
            updateRedisHashField(getKey(), "mainTriggerNum", this.mainTriggerNum);
        }
    }

    public void addMainTriggerNum(int value) {
        this.mainTriggerNum += value;
        updateRedisHashField(getKey(), "mainTriggerNum", this.mainTriggerNum);
    }

    public long getTriggerTime() {
        return triggerTime;
    }

    public void putTriggerTime(long triggerTime) {
        if (this.triggerTime != triggerTime) {
            this.triggerTime = triggerTime;
            updateRedisHashField(getKey(), "triggerTime", this.triggerTime);
        }
    }

    public void addTriggerTime(long value) {
        this.triggerTime += value;
        updateRedisHashField(getKey(), "triggerTime", this.triggerTime);
    }

    public JsonArray getBuyListArray() {
        if (this.buyList != null && this.buyList.length() > 0) {
            return new JsonArray(this.buyList);
        } else {
            return new JsonArray();
        }
    }

    public String getBuyList() {
        return buyList;
    }

    public void putBuyList(String buyList) {
        if (!this.buyList.equals(buyList)) {
            this.buyList = buyList;
            updateRedisHashField(getKey(), "buyList", this.buyList);
        }
    }

}